package com.qf.user.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.dto.PageDto;
import com.qf.user.entity.User;
import com.qf.user.mapper.UserMapper;
import com.qf.user.service.UserService;
import com.qf.utils.JWTUtil;
import com.qf.vo.PageResult;
import com.qf.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 * 管理员用户表 服务实现类
 * </p>
 *
 * @author XJJ
 * @since 2023-10-30
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    /*用户注册*/
    @Override
    public Result<User> addUser(User user) {
        //1、通过输入用户名查询数据库中该对象是否存在，如果存在提示该对象已经存在（该对象在数据库中存在的）
        String userName=user.getUserName();
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUserName,userName);
        User userDb = userMapper.selectOne(queryWrapper);
        if (userDb!=null){
            return new Result<>(1,"用户名已经存在");
        }
        //2、将用户信息添加到数据库中
        String userPassword=user.getPassword();//前端输入的密码
        user.setPassword(SecureUtil.md5(userPassword));
        user.setEnable(1);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        user.setDeleted(1);
        userMapper.insert(user);
        return new Result<>(0,"success");
    }


    public Result login(User user) {
        if (user == null) {
            return new Result(1, "非法参数");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUserName, user.getUserName());
        User userDb = this.baseMapper.selectOne(queryWrapper);
        if (userDb == null) {
            return new Result(1, "用户名或者密码不正确");
        }
        //比较密码
        String md5 = SecureUtil.md5(user.getPassword());
        if (md5.equals(userDb.getPassword())) {
            //颁发JWT令牌
            userDb.setPassword("");
            String token = JWTUtil.genTokenExpireByHour(userDb, 2);
            return new Result(0, "登入成功!!!", token);
        }

        return new Result(1, "用户名或者密码不正确");
    }
    //用户分页查询
    public PageResult<User> page(PageDto<User> pageDto){
        final Page<User> objectPage = new Page<>(pageDto.getNow(), pageDto.getSize());
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        if (!StringUtils.isEmpty(pageDto.getWhere().getEmail())){
            queryWrapper.lambda().like(User::getEmail,pageDto.getWhere().getEmail());
        }
        IPage<User> UserIPage=this.baseMapper.selectPage(objectPage,queryWrapper);
        PageResult<User> pageResult=new PageResult<>();
        pageResult.setTotal(UserIPage.getTotal());
        pageResult.setPages(UserIPage.getPages());
        pageResult.setList(UserIPage.getRecords());
        return pageResult;
    }
}
